package com.finance.util;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期工具类
  *
 */
public class DateUtil {
	protected static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

	public static final String YEAR_AND_MONTH_FORMAT ="yyyy-MM";
 	public static final String SIMPLE_FORMAT ="yyyyMMdd";
	public static final String SIMPLE_SECOND_FORMAT ="yyyyMMddHHmmss";
 	public static final String DEFAULT_FORMAT ="yyyy-MM-dd HH:mm:ss";
	public static final String DAY_FORMAT ="yyyy-MM-dd";
 	public static final String DEFAULT_MINUTE_FORMAT ="yyyy-MM-dd HH:mm";
	public static final String LOCAL_FORMAT ="yyyy年MM月dd日HH时mm分ss秒";
	public static final String LOCAL_DATE_FORMAT ="yyyy年MM月dd日HH时mm分ss秒";
	public static final String LOCAL_TIME_FORMAT ="HH时mm分ss秒";

	/**
	 * 将日期转换为字符串
	 * @return
	 */
	public static String dateFormat(){
		return dateFormat(new Date(),DEFAULT_FORMAT);
	}
	public static String dateMinuteFormat(){
		return dateFormat(new Date(),DEFAULT_MINUTE_FORMAT);
	}
	public static String dateFormat(String pattern){
		return dateFormat(new Date(), pattern);
	}
	public static String dateFormat(Date date,String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	public static String dateFormat(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
		return sdf.format(date);
	}
	
	/**
	 * 字符串转换为日期
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static Date str2date(String source) throws ParseException{
 		return str2date(source,DEFAULT_FORMAT);
	}

	/**
	 * 字符串转换为日期
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static Date str2date(String source,String pattern) throws ParseException{
 		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
 		return sdf.parse(source);
	}
	public static Date addMinute(Date date,Integer minutes){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, minutes);
		return c.getTime();
	}

	/**
	 * 将时间移动days天
	 *
	 * @param date 当前日期
	 * @param days 移动的天数
	 * @return 移动后的时间
	 */
	public static Date addDay(Date date, Integer days) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, days);
		return c.getTime();
	}

	/**
     * 增加月
     * @param date
     * @param i
     * @return
     */
    public static Date addMonth(Date date, int i) {
        return org.apache.commons.lang.time.DateUtils.addMonths(date, i);
    }
    
    /**
     * 增加小时
     * @param date
     * @param amount
     * @return
     */
    public static Date addHours(Date date,int amount){
    	return org.apache.commons.lang.time.DateUtils.addHours(date, amount);
    }

    /**
     * 比较dt1和dt2的时间差
     * @param dt1
     * @param dt2 未指定时，默认为当前时间
     * @return 3分钟 -2秒等
     * @throws ParseException
     */
    public static String diff(Date dt1,Date dt2) throws ParseException{
    	String time="";
    	/**未指定对比时间时，和当前时间做比较**/
  		dt2 = dt2==null?  new Date() :dt2;
 		long between = dt2.getTime() - dt1.getTime();
		long day = between / (24 * 60 * 60 * 1000);
		long hour = (between / (60 * 60 * 1000) - day * 24);
		long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long second = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		long month = day / 30;
		long year = month/12;
		if(Math.abs(year)< 1){
			if ( Math.abs(month)< 1) {
				if ( Math.abs(day)< 1) {
					if ( Math.abs(hour) < 1) {
						if ( Math.abs(min) < 1) {
							time = second + "秒";
							return time;
						}
						time = min + "分钟";
						return time;
					} else {
						time = hour + "小时";
						return time;
					}
				} else {
					time = day + "天";
					return time;
				}
			}
			else{
				time =  month + "个月";
				return time;
			}
		}
		else {
			time = year + "年";
			return time;
		}
    }
    public static String getDiff(Date dt1,Date dt2) throws ParseException{
    	String time="";
    	/**未指定对比时间时，和当前时间做比较**/
    	dt2 = dt2==null?  new Date() :dt2;
     	long between = dt2.getTime() - dt1.getTime();
 		long hourUnit = 1000*60*60;
		long minutUnit = 1000*60;
		long secondUnit = 1000;
		long hour = between/hourUnit;
		long min = (between%hourUnit)/minutUnit;
		long sec = (between%minutUnit)/secondUnit;

		if(Math.abs(hour)>0){
			time+=hour+"时";
		}
		if(Math.abs(min)>0){
			time+=min+"分";
		}
		if(Math.abs(sec)>0){
			time+=sec+"秒";
		}
		if(StringUtils.isBlank(time)) {
            time = "0秒";
        }
		return time;
     }
    /**
	 * 获得当前时间与指定时间的时间差
	 * @param date
	 * @return 3分钟 2秒等
	 * @throws ParseException
	 */
	public static String getDiff(Date date) throws ParseException {
		return getDiff(date,null);
	}

	/**
	 * Long类型日期转为指定格式字符串
	 * @param currentTime
	 * @return
	 */
	public static String long2Str(Long currentTime,String formate){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formate);
		return simpleDateFormat.format(currentTime);
	}

	/**
	 * Description	： 比较两个时间的大小
	 * @author "hejp"  2015年3月11日 下午7:59:28
	 * @return
	 *
	 */
	public static int compare_date(Date date1, Date date2) {
		try {
			if (date1.getTime() > date2.getTime()) {
				//date1时间晚于date2时间
				return 1;
			} else if (date1.getTime() < date2.getTime()) {
				//date1时间早于date2时间
				return -1;
			} else {
				return 0;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return 0;
	}

	public static List<String> genDays(Date date){
		String yyyyMM = DateUtil.dateFormat(date,"yyyyMM");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		List<String> list = new ArrayList<String>();
		for(int i =1 ;i <=max; i++){
			if(i<10) {
				list.add(yyyyMM+"0"+i);
			}else{
				list.add(yyyyMM+i);
			}
		}
		return list;
	}

	/**
	 * 计算时间区间
	 * @param fetchDate
	 * @return
	 */
	public static Map<String,Object> getTimeInterval(Date fetchDate){
		Map<String,Object> dateMap = new HashMap<String,Object>();
		Date now = new Date(System.currentTimeMillis());
		Calendar from = Calendar.getInstance();
		Calendar to = Calendar.getInstance();
		if(fetchDate.getMonth() < now.getMonth()){
			from.setTime(fetchDate);
			to.setTime(fetchDate);
			to.roll(Calendar.DAY_OF_MONTH, -1);
		}else if(0 == DateUtil.compare_date(now,fetchDate) || 1 == DateUtil.compare_date(now,fetchDate)){
			from.setTime(fetchDate);
			//from.add(Calendar.DATE,-1);
			to.setTime(now);
			to.add(Calendar.DATE,-1);
		}else{
			from.setTime(now);
			from.set(Calendar.DAY_OF_MONTH, 1);
			to.setTime(now);
			to.add(Calendar.DATE,-1);
		}
		from.set(Calendar.DAY_OF_MONTH,1);
		from.set(Calendar.HOUR_OF_DAY,0);
		from.set(Calendar.MINUTE,0);
		from.set(Calendar.SECOND,0);
		to.set(Calendar.HOUR_OF_DAY,23);
		to.set(Calendar.MINUTE,59);
		to.set(Calendar.SECOND,59);
		dateMap.put("fromDate",new Date(from.getTimeInMillis()));
		dateMap.put("toDate",new Date(to.getTimeInMillis()));
		return dateMap;
	}

	public static Date parse(Date date,String format) throws ParseException{
		Date date1 = new Date(date.getTime());
		String str = dateFormat(date1,format);
		return str2date(str,format);
	}
}


